package com.zouhao.main;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.Collection;
import java.util.Iterator;
import java.util.Properties;
import java.util.Scanner;

import jxl.CellType;
import jxl.Workbook;
import jxl.format.Alignment;
import jxl.format.Border;
import jxl.format.BorderLineStyle;
import jxl.format.Colour;
import jxl.format.UnderlineStyle;
import jxl.format.VerticalAlignment;
import jxl.write.Formula;
import jxl.write.Label;
import jxl.write.Number;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

import org.tmatesoft.svn.core.SVNException;
import org.tmatesoft.svn.core.SVNLogEntry;

import com.zouhao.util.SVNUtil;

public class run {
	private static long prevStartVision;//起始版本的上一个版本,主要用于diff比较差异
	private static int startVision;//起始版本号
	private static int endVision;//结束版本号
	private static String batUrl;//批处理文件路径
	private static String rootPath;//项目文件路径
	private static String username;//svn账号
	private static String password;//svn密码
	private static String svnRoot;//svn地址
	private static void setConfig(){
		try {   
			//从配置文件获取svn仓库地址,用户名和密码
			Properties prop = new Properties();   
			InputStream in = Object.class.getResourceAsStream("/config.properties");   
			prop.load(in);
			batUrl=System.getProperty("user.dir")+"\\"+prop.getProperty("batName").trim();
			prevStartVision=Long.valueOf(prop.getProperty("prevStartVision").trim());
			startVision=Integer.valueOf(prop.getProperty("startVision").trim());
			endVision=Integer.valueOf(prop.getProperty("endVision").trim());
			rootPath=prop.getProperty("rootPath").trim();
			svnRoot=prop.getProperty("svnRoot").trim();
			username=prop.getProperty("username").trim();
			password=prop.getProperty("password").trim();
		} catch (IOException e) {   
			e.printStackTrace();   
		}
	}
	/**
	 * 获取svn日志相应信息
	 * @param start 起始版本
	 * @param end   结束版本
	 * @return
	 */
	private static Collection<SVNLogEntry> getCollectionLog(int start,int end){
		SVNUtil svnUtil=new SVNUtil();
		svnUtil.login(svnRoot,username,password);
		return svnUtil.getCollectionLog(start, end);
	}
	/**
	 * 顶部文字样式
	 * @return
	 * @throws WriteException
	 */
	private static WritableCellFormat getHeadCellFormat() throws WriteException{
		WritableCellFormat wc = new WritableCellFormat(new WritableFont(WritableFont.createFont("宋体"),11,WritableFont.BOLD,
				false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK));
		wc.setBackground(Colour.AQUA);
		wc.setAlignment(Alignment.CENTRE);
		wc.setBorder(Border.ALL, BorderLineStyle.THIN,Colour.BLACK);
		wc.setVerticalAlignment(VerticalAlignment.CENTRE);
		return wc;
	}
	private static WritableCellFormat getFirstCellFormat() throws WriteException{
		WritableCellFormat wc = new WritableCellFormat(new WritableFont(WritableFont.createFont("宋体"),11,WritableFont.NO_BOLD,
				false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK));
		wc.setBackground(Colour.WHITE);
		return wc;
	}

	private static WritableCellFormat getSecondCellFormat() throws WriteException{
		WritableCellFormat wc = new WritableCellFormat(new WritableFont(WritableFont.createFont("宋体"),11,WritableFont.NO_BOLD,
				false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK));
		wc.setBackground(Colour.LIGHT_GREEN);
		return wc;
	}
	private static WritableCellFormat getTotalCelFormat() throws WriteException{
		WritableCellFormat wc = new WritableCellFormat(new WritableFont(WritableFont.createFont("宋体"),11,WritableFont.NO_BOLD,
				false,UnderlineStyle.NO_UNDERLINE,jxl.format.Colour.BLACK));
		return wc;
	}

	public static void main(String arge[]) throws SVNException, IOException, RowsExceededException, WriteException{
		setConfig();
		WritableWorkbook workbook = Workbook.createWorkbook(new File(System.getProperty("user.dir")+"\\"+"代码审核.xls"));
		WritableSheet detailSheet = workbook.createSheet("详细信息",1);
		/**详细信息头部生成*/
		detailSheet.setRowView(0, 600); //设置行高
		detailSheet.addCell(new Label(0,0,"SVN信息",getHeadCellFormat()));
		detailSheet.addCell(new Label(1,0,"修改代码行数",getHeadCellFormat()));
		detailSheet.addCell(new Label(2,0,"代码质量基数（每次提交基数加1）",getHeadCellFormat()));
		detailSheet.addCell(new Label(3,0,"代码质量评分（合格1；不合格0）",getHeadCellFormat()));
		detailSheet.addCell(new Label(4,0,"注释质量基数（每次提交基数加1）",getHeadCellFormat()));
		detailSheet.addCell(new Label(5,0,"注释质量评分（合格1；不合格0）",getHeadCellFormat()));
		detailSheet.addCell(new Label(6,0,"备注",getHeadCellFormat()));
		/**设置第一列的宽度*/
		detailSheet.setColumnView(0,150);
		detailSheet.setColumnView(1,20);
		detailSheet.setColumnView(2,30);
		detailSheet.setColumnView(3,30);
		detailSheet.setColumnView(4,30);
		detailSheet.setColumnView(5,30);
		detailSheet.setColumnView(6,50);
		/**详细信息头部生成结束*/
		Collection<SVNLogEntry> logEntries = getCollectionLog(startVision,endVision);
		int i=0;
		boolean first=true;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 hh:mm:ss");

		long current;
		for ( Iterator<SVNLogEntry> entries = logEntries.iterator( ); entries.hasNext( ); ) {
			WritableCellFormat cellFormat=first?getFirstCellFormat():getSecondCellFormat();
			SVNLogEntry logEntry = ( SVNLogEntry ) entries.next( );
			detailSheet.addCell(new Label(0,++i,"Revision: " + logEntry.getRevision( ),cellFormat));
			autoComplateCellFormat(detailSheet,i,cellFormat);
			detailSheet.addCell(new Label(0,++i,"Author: " + logEntry.getAuthor( ),cellFormat));
			autoComplateCellFormat(detailSheet,i,cellFormat);
			detailSheet.addCell(new Label(0,++i,"Date: " + sdf.format(logEntry.getDate()),cellFormat));
			autoComplateCellFormat(detailSheet,i,cellFormat);
			detailSheet.addCell(new Label(0,++i,"Message",cellFormat));
			/**计算修改代码行数*/
			current=logEntry.getRevision();
			Process process = Runtime.getRuntime().exec(batUrl+" "+prevStartVision+" "+current+" "+rootPath+" "+rootPath.substring(0,2));
			InputStream fis = process.getInputStream();
			Scanner sc=new Scanner(fis);
			int updateRow=0;
			while(sc.hasNext()){
				String line=sc.next();
				if(line.contains("+++")||line.contains("---")){
					continue;
				}
				if(line.length()<=1){
					continue;
				}
				if(line.substring(0,1).equals("+")||line.substring(0,1).equals("-")){
					++updateRow;
				}
			}
			process.destroy();
			prevStartVision=current;
			/**计算修改代码行数结束*/

			detailSheet.addCell(new Number(1,i,updateRow,cellFormat));
			detailSheet.addCell(new Number(2,i,1,cellFormat));
			detailSheet.addCell(new Number(3,i,1,cellFormat));
			detailSheet.addCell(new Number(4,i,1,cellFormat));
			detailSheet.addCell(new Number(5,i,1,cellFormat));
			detailSheet.addCell(new Label(6,i,"",cellFormat));


			detailSheet.addCell(new Label(0,++i,logEntry.getMessage( ),cellFormat));
			autoComplateCellFormat(detailSheet,i,cellFormat);
			detailSheet.addCell(new Label(0,++i,"----",cellFormat));
			autoComplateCellFormat(detailSheet,i,cellFormat);
			detailSheet.addCell(new Label(0,++i,"",cellFormat));
			autoComplateCellFormat(detailSheet,i,cellFormat);
			first=first?false:true;
		}
		/**详细信息生成结束*/
		/**设置第一列的宽度*/
		WritableSheet infoSheet = workbook.createSheet("预览信息",0);
//		infoSheet.setColumnView(0,20);
//		infoSheet.setColumnView(1,20);
//		infoSheet.setColumnView(2,20);
//		infoSheet.setColumnView(3,20);
//		infoSheet.setColumnView(4,20);
//		infoSheet.setColumnView(5,20);
//		infoSheet.setColumnView(6,20);
//		/**生成第一行*/
//		infoSheet.addCell(new Label(0,0,"SVN迭代数",getHeadCellFormat()));
//		infoSheet.addCell(new Label(1,0,"修改代码数",getHeadCellFormat()));
//		infoSheet.addCell(new Label(2,0,"代码合格率",getHeadCellFormat()));
//		infoSheet.addCell(new Label(3,0,"代码不合格提交次数",getHeadCellFormat()));
//		infoSheet.addCell(new Label(4,0,"注释合格率",getHeadCellFormat()));
//		infoSheet.addCell(new Label(5,0,"注释不合格提交次数",getHeadCellFormat()));
//		infoSheet.addCell(new Label(6,0,"备注",getHeadCellFormat()));
//		/**生成第二行*/
//		infoSheet.addCell(new Formula(0,1,"详细信息A)",getTotalCelFormat()));
		workbook.write();
		workbook.close();
	}
	private static void autoComplateCellFormat(WritableSheet sheet,int i,WritableCellFormat format) throws RowsExceededException, WriteException{
		for(int k=1;k<=6;k++){
			sheet.addCell(new Label(k,i,"",format));
		}
	}
}
